package com.epro.dx.task;

import android.content.Context;
import android.os.AsyncTask;

import com.epro.dx.listenner.MyAsyncTaskListenner;
import com.epro.dx.util.ActivityUtil;
import com.epro.dx.util.OnekeyFilterLocalUtil;
import com.weipu.common.constants.ServerConstant;
import com.weipu.common.facade.exception.DxException;
import com.weipu.common.facade.factory.ProviderFactory;
import com.weipu.common.facade.model.ProductModel;
import com.weipu.common.util.StringUtil;

import java.util.ArrayList;

/**
 * @author tony
 *         根据sku获取产品列表的任务
 */
public class GetProductsFromSkusAsyncTask extends AsyncTask<Void, Void, String> {

    private ArrayList<ProductModel> bfdProducts;
    private ArrayList<Integer> skuList;
    private Context mContext;
    private MyAsyncTaskListenner myAsyncTaskListenner;
    private int pageIndex;
    private int pageSize;
    private String orderbyField;
    private String categoryid;
    private boolean isFilter;

    public GetProductsFromSkusAsyncTask(ArrayList<Integer> skuList, Context mContext,
                                        int pageIndex, int pageSize, String orderbyField, int categoryid, boolean isFilter, MyAsyncTaskListenner myAsyncTaskListenner) {
        this.skuList = skuList;
        this.mContext = mContext;
        this.pageIndex = pageIndex;
        this.pageSize = pageSize;
        this.orderbyField = orderbyField;
        this.myAsyncTaskListenner = myAsyncTaskListenner;
        this.isFilter = isFilter;
        if (categoryid == -2) {
            this.categoryid = OnekeyFilterLocalUtil.getLocalCategoryIds(mContext);
        } else if (categoryid == -3) {
            this.categoryid = OnekeyFilterLocalUtil.getCategoryIds();
        } else {
            this.categoryid = String.valueOf(categoryid);

        }
    }

    @Override
    protected void onPreExecute() {
        myAsyncTaskListenner.onPreTask();
    }

    @Override
    protected String doInBackground(Void... params) {
        String currencyCode = StringUtil.getConfig(ServerConstant.Parameter.PARAMETER_NAME_CURRENCYCODE, "USD");
        try {
            bfdProducts = ProviderFactory.createCurrencyServiceProvider().switchProductCurrency(skuList, currencyCode, pageIndex, pageSize, orderbyField,
                    ActivityUtil.getCustomerID(), isFilter, categoryid);
        } catch (DxException e) {
            return e.getErrCode();
        }
        return "0";
    }

    @Override
    protected void onPostExecute(String result) {
        myAsyncTaskListenner.onPostTask(result, bfdProducts);
    }
}
